<template>
  <div class="login_box">
      <div class="login_top_btn">
          <!--<a>忘记密码</a>-->
          <!--<router-link to="/register" style="color:#fff;">注 册</router-link>-->
      </div>
      <div class="login_logo align_center" style="padding-top: 5%;">
            <!--<img src="../assets/images/logo.png" alt="" height="74px" style="height:74px">-->
            <!--赛维O2O业务后台管理平台-->
            <img src="../assets/images/login_title.png" alt="">
      </div>
      <div class="login_main">
        
        <input class="width_full" type="text" placeholder="请输入帐号" v-model="userName" @keyup.13="keyDown($event)">
        <input class="width_full" type="password" placeholder="请输入密码" v-model="password" @keyup.13="keyLogin($event)" ref="inputPsw">
        <el-row :gutter="20">
            <!--<el-col :span="12" style="line-height: 44px;margin-top:10px;text-align: center">
                <router-link to="/register" style="color:#fff;">注 册</router-link>
            </el-col>-->
            <el-col :span="24">
                <input class="width_full" type="button" value="登 录" @click="login" ref="loginBtn">
            </el-col>
        </el-row>
      </div>
      <div class="login_bottom">
        <!--Copyright © 青岛红柿子软件技术有限公司版权所有 备案号：鲁ICP备14000000-1号-->
      </div>
  </div>
</template>

<script>
    
    export default {
        // 在components字段中，包含导入的子组件
        name:'app',
        data() {
            return {
                userName: '',
                password: '',
                logonMode: 1
            }
        },
        components: {
        },
        methods: {
            login() {
                this.$http.post(API.oauth, 
                        {
                            clientId: "098f6bcd4621d373cade4e832627b4f6", 
                            accountName: this.userName, 
                            accountPwd: this.password,
                            logonMode: this.logonMode
                        }, 
                        {
                            emulateJSON: true
                        }
                    ).then((response) => {
                    console.log(response.body);

                    if (response.body.status == 0) {
                        var token = response.body.data.accessToken;
                        console.log("token", token);
                        localStorage.Authorization = token;
                        this.setCookie("token", token, 1);
                        localStorage.userId = response.body.data.userId; 
                        localStorage.deptId = response.body.data.deptId;
                        localStorage.userName = response.body.data.realName;
                        localStorage.avatar = response.body.data.avatar;
                        localStorage.perms = response.body.data.perms;
                        console.log(localStorage.avatar);
                        //查询Company信息
                        // this.$http.get(API.rootPath + "/companies/depts/id/" + localStorage.deptId).then((response) => {
                        //     if (response.body.status == 0) {
                        //         localStorage.companyId = response.body.data.id;
                        //         localStorage.companyName = response.body.data.name;
                                
                        //         // console.log("登陆完初始化头部啦", localStorage.userName);
                                
                        //     }
                        // })

                        eventBus.$emit('initLeftMenu');
                                eventBus.$emit('initTopBar');
                                this.$router.push("/");
                        
                    } else{
                        this.$message.error(response.body.msg);
                    }
                }, response => {

                });
            },
            keyLogin(ev){
//                if (ev.keyCode == 13)  //回车键的键值为13
                    // console.log('你按回车键了');
                    this.$refs.loginBtn.click(); //调用登录按钮的登录事件
             },
            keyDown(ev){
                // console.log('你按方向下键了');
                this.$refs.inputPsw.focus(); //输入密码框获取焦点
            }
    },
        created(){
            // this.keyLogin()
        }
    }
</script>